/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ztewatch.wearcontacts.loader;

import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.provider.ContactsContract.Groups;

/**
 * A loader for use in the default contact list, which will also query for the
 * user's group if configured to do so.
 */
public class ContactsAndGroupLoader extends CursorLoader {
	private Context mContext;

	public ContactsAndGroupLoader(Context context) {
		super(context);
		mContext = context;
	}

	public void setLoadProfile(boolean flag) {
	}

	public void setProjection(String[] projection) {
		super.setProjection(projection);
	}

//	@Override
//	public Cursor loadInBackground() {
//		List<Cursor> cursors = new ArrayList<Cursor>();
//		Cursor cursor = null;
//		try {
//			cursor = super.loadInBackground();
//		} catch (NullPointerException e) {
//		}
//
//		// try {
//		// add history start
//		MatrixCursor matrix = new MatrixCursor(ContactQuery.CONTACT_PROJECTION);
//		Object[] addHistory = new Object[ContactQuery.CONTACT_PROJECTION.length];
//		for (int i = 0; i < addHistory.length; i++) {
//			if (Contacts.DISPLAY_NAME_PRIMARY
//					.equals(ContactQuery.CONTACT_PROJECTION[i])) {
//				addHistory[i] = mContext.getString(R.string.history_str);
//			}
//		}
//		matrix.addRow(addHistory);
//		// add history end
//		Object[] row = new Object[ContactQuery.CONTACT_PROJECTION.length];
//		while (cursor.moveToNext()) {
//			for (int i = 0; i < row.length; i++) {
//				row[i] = cursor.getString(i);
//			}
//			matrix.addRow(row);
//		}
//
//		cursors.add(matrix);
//		cursors.add(loadGroup());
//		return new MergeCursor(cursors.toArray(new Cursor[cursors.size()]));
//	}

	private MatrixCursor loadGroup() {
		Cursor cursor = getContext().getContentResolver().query(
				GroupListLoaderUtil.GROUP_LIST_URI,
				GroupListLoaderUtil.COLUMNS,
				Groups.ACCOUNT_TYPE + " NOT NULL AND " + Groups.ACCOUNT_NAME
						+ " NOT NULL AND " + Groups.AUTO_ADD + "=0 AND "
						+ Groups.FAVORITES + "=0 AND " + Groups.DELETED + "=0",
				null,
				Groups.ACCOUNT_TYPE + ", " + Groups.ACCOUNT_NAME + ", "
						+ Groups.DATA_SET + ", " + Groups.TITLE
						+ " COLLATE LOCALIZED ASC");
		if (cursor == null) {
			return null;
		}
		MatrixCursor matrix = new MatrixCursor(GroupListLoaderUtil.COLUMNS);
		Object[] row = new Object[GroupListLoaderUtil.COLUMNS.length];
		while (cursor.moveToNext()) {
			for (int i = 0; i < row.length; i++) {
				row[i] = cursor.getString(i);
			}
			matrix.addRow(row);
		}
		return matrix;
	}

}
